﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Common
{
    public static class WordParser
    {

        public static char[] ReplaceCharArray = { ' ', '?', ',', ';', ':', '.', '(', ')', '[', ']', '%', '*', '$', '-' };

        /// <summary>
        /// Parses a single word, removes unnecessary characters and stems the word (if in english)
        /// </summary>
        /// <param name="Text"></param>
        /// <returns></returns>
        public static string ParseWord(string Text)
        {
            Stemmer.PorterStemmer stemmer = new Stemmer.PorterStemmer();
            //Strip text.
            Text = Text.Trim(ReplaceCharArray);
            Text = Text.Trim('\"', '\''); //This should only be trimmed if appears before/after word.
            //Apply Stemmer (for english)
            Text = Text.ToLower();
            Text = stemmer.StemWord(Text);
            //Apply stopword filter - for english. (not using this, for now)
           // if (Stemmer.StopWordsHandler.IsStopword(Text))
            //    return String.Empty;
            return Text;
        }


        /// <summary>
        /// Parses a text phrase into an array (applies ParseWord to each word in the phrase).
        /// </summary>
        /// <param name="Text"></param>
        /// <returns></returns>

        public static string[] GetParsedPhraseArray(string Text)
        {
            Regex r = new Regex(@"\s+");            //remove all whitespace
            Text = r.Replace(Text, " ");// to a single space
            foreach (char c in ReplaceCharArray)
                Text = Text.Replace(c, ' ');
            string[] PhraseArray =  Text.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < PhraseArray.Length; i++)
            {
                PhraseArray[i] = ParseWord(PhraseArray[i]);
            }
            return PhraseArray;
           
        }
    }
}
